package com.woniuxy.lrz.carlproject.controller;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniuxy.lrz.carlproject.common.util.Results;
import com.woniuxy.lrz.carlproject.controller.form.formcar.CarAddOrUpdateForm;
import com.woniuxy.lrz.carlproject.controller.form.formcar.CarBrandFindForm;
import com.woniuxy.lrz.carlproject.model.CarBrand;
import com.woniuxy.lrz.carlproject.service.CarBrandService;
import com.woniuxy.lrz.carlproject.service.CarTypeService;
import com.woniuxy.lrz.carlproject.service.dto.dtocar.CarBrandDto;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 汽车品牌表 前端控制器
 * </p>
 *
 * @author lrz
 * @since 2022-09-28
 */
@RestController
@Slf4j
@Api(tags = "车辆品牌接口")
@RequestMapping("/carlproject/carBrand")
@Validated
public class CarBrandController {
    @Resource
    private CarBrandService carBrandService;

    @PostMapping("/addOrUpdateCarBrand")
    @ApiOperation("新增或修改车辆品牌")
    public Results addOrUpdateCarBrand(@RequestBody @Valid CarAddOrUpdateForm carAddOrUpdateForm){
        carBrandService.addOrUpdateCarBrand(carAddOrUpdateForm);
        return Results.instance().setMessage("操作成功");
    }

    @DeleteMapping("/deleteCarBrand")
    @ApiOperation("删除车辆品牌")
    @ApiImplicitParam(name = "carBrandId",value = "品牌Id")
    public Results deleteCarBrand(@NotNull(message = "品牌Id不能为空") Integer carBrandId){
        boolean b = carBrandService.deleteCarBrand(carBrandId);
        return Results.instance().setMessage("删除成功");
    }

    @GetMapping("/findCarBrand")
    @ApiOperation("加载品牌列表")
    public Results findCarBrand(@Valid CarBrandFindForm carBrandFindForm){
        Page<CarBrandDto> carBrand = carBrandService.findCarBrand(carBrandFindForm);
        return Results.instance().setMessage("展示品牌列表成功").setData(carBrand);
    }

    @GetMapping("/findCarBrandCondition")
    @Cacheable(value = "car",key = "'findBrand:'")
    @ApiOperation("加载品牌下拉列表，并存储到redis中")
    public Results findCarBrandCondition(){
        List<CarBrand> list = carBrandService.list();
        List<CarBrandDto> carBrandDtos = BeanUtil.copyToList(list, CarBrandDto.class);
        return Results.instance().setData(carBrandDtos).setMessage("加载品牌下拉列表成功");
    }




}
